package com.qyhl.webtv.module_circle.circle.usercenter;


import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.circle.CircleUCenterBean;
import com.qyhl.webtv.commonlib.entity.circle.ShieldResultBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/24.
 */

public class CircleUserModel implements CircleUserContract.CircleUserModel {
    private CircleUserPresenter mPresenter;

    public CircleUserModel(CircleUserPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取用户中心内容
     *
     * @param username
     * @param dstUsername
     */
    @Override
    public void getCenterInfo(String username, String dstUsername, final String lastId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";

        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=ucenter" + "&username=" + userName + "&dstUserName=" + dstUsername + "&lastTopicId=" + lastId
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<CircleUCenterBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getError(1, "解析异常，加载失败！");
                    }

                    @Override
                    public void onSuccess(CircleUCenterBean circleUCenterBean) {
                        mPresenter.getSuccess(circleUCenterBean);
                    }
                });



    }

    /**
     * 获取更多信息
     *
     * @param username
     * @param dstUsername
     * @param lastId
     */
    @Override
    public void getMoreInfo(String username, String dstUsername, String lastId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=moreUserTopic" + "&username=" + userName + "&dstUserName=" + dstUsername + "&lastTopicId=" + lastId
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getError(2, "解析异常，获取失败！");
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean> circleHomeBeans) {
                        if (circleHomeBeans != null && circleHomeBeans.size() > 0) {
                            mPresenter.setMoreInfo(circleHomeBeans);
                        } else {
                            mPresenter.getError(3, "no more");
                        }
                    }
                });

    }


    /**
     * 关注
     *
     * @param dstUserName
     */
    @Override
    public void doFollow(String dstUserName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite()+ "&method=follow" + "&username=" + userName + "&dstUsername=" + dstUserName
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.followError("网络异常，关注失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.followSuccess("关注成功！");
                        } else {
                            mPresenter.followError("解析异常，关注失败！");
                        }
                    }

                });

    }

    /**
     * 取消关注
     *
     * @param dstUserName
     */
    @Override
    public void undoFollow(String dstUserName) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=unfollow" + "&username=" +userName + "&dstUsername=" + dstUserName
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.unFollowError("网络异常，取关失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.unFollowSuccess("取关成功！");
                        } else {
                            mPresenter.unFollowError("解析异常，取关失败！");
                        }

                    }
                });

    }

    /**
     * 屏蔽用户
     *
     * @param dstUsername
     */
    @Override
    public void doBlock(String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=block" + "&username=" + userName + "&dstUsername=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.blockError("网络异常，屏蔽失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.blockSuccess("您已屏蔽该用户！");
                        } else {
                            mPresenter.blockError("解析异常，屏蔽失败！");
                        }
                    }
                });
    }

    /**
     * 解除屏蔽
     *
     * @param dstUsername
     */
    @Override
    public void undoBlock(String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=unblock" + "&username=" + userName + "&dstUsername=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.unBlockError("网络异常，取消失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.unBlockSuceess("解除屏蔽成功！");
                        } else {
                            mPresenter.unBlockError("解析异常，取消失败！");
                        }
                    }
                });


    }

    @Override
    public void checkShield(String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=mailPermit" + "&username=" + userName + "&dstUsername=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<ShieldResultBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.checkResult(0, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(ShieldResultBean shieldResultBean) {
                        if (shieldResultBean.isDefriend()) {
                            mPresenter.checkResult(0, "对不起，对方拒绝和你聊天！");
                        } else if (shieldResultBean.isGag()) {
                            mPresenter.checkResult(0, "对不起，管理员禁言了你！");
                        } else {
                            mPresenter.checkResult(1, "success");
                        }
                    }
                });

    }


}
